package com.example.a17376009_wangzehui_finaltest;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class PetAdapter  extends RecyclerView.Adapter<PetAdapter.ViewHolder>{
    private Context mContext;
    private List<Pet> mPetList;
    static class ViewHolder<CardView> extends  RecyclerView.ViewHolder{
        CardView cardView;
        ImageView petImage;
        TextView petName;

        public ViewHolder(View view){
            super(view);
            cardView=(CardView) view;
            petImage=(ImageView) view.findViewById(R.id.pet_image);
            petName=(TextView) view.findViewById(R.id.pet_name);
        }
    }
    public PetAdapter(List<Pet> petList){

        mPetList=petList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent,int viewType){
        if (mContext==null){
            mContext=parent.getContext();
        }
        View view= LayoutInflater.from(mContext).inflate(R.layout.pet_item,parent,false);

        return  new ViewHolder(view);

        final  ViewHolder holder=new ViewHolder(view);
        holder.cardView.toString(new View.OnClickListener(){
            @Override
            public  void onClick(View v){
                int position=holder.getAdapterPosition();
                Pet pet=mPetList.get(position);
                Intent intent=new Intent(mContext,PetActivity.class);
                intent.putExtra(PetActivity.PET_NAME,pet.getName());
                intent.putExtra(PetActivity.PET_IMAGE_ID,pet.getImageId());
                mContext.startActivity(intent);
            }
        });
        return  holder;
    }
    @Override
    public void onBindViewHolder(ViewHolder holder,int position){
        Pet pet =mPetList.get(position);
        holder.petName.setText(pet.getName());
        Glide.with(mContext).load(pet.getImageId()).into(holder.petImage);
    }
    @Override
    public int getItemCount(){
    return mPetList.size();
    }
}
